#include "StdAfx.h"
#include "sa_TwMeshLoad.h"
#include "TwEngineDx.h"




sa_TwMeshLoad::sa_TwMeshLoad(void)
{
}

sa_TwMeshLoad::~sa_TwMeshLoad(void)
{
}
void sa_TwMeshLoad::InitGraphics()
{
	D3DXCreateTeapot(g_Render->GetDevice(), &meshTeapot, NULL);    // create the teapot
}

void sa_TwMeshLoad::InitRenderState()
{
	g_Render->GetDevice()->SetRenderState(D3DRS_ZENABLE, TRUE);
	g_Render->GetDevice()->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));

	g_Render->GetDevice()->SetSamplerState(0, D3DSAMP_MAXANISOTROPY, 8);    // anisotropic level
	g_Render->GetDevice()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);    // minification
	g_Render->GetDevice()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);    // magnification
	g_Render->GetDevice()->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);    // mipmap

}

void sa_TwMeshLoad::Update()
{
}

void sa_TwMeshLoad::Render()
{
	g_Render->GetDevice()->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
	g_Render->GetDevice()->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

	g_Render->GetDevice()->BeginScene();

	// select which vertex format we are using
	//d3ddev->SetFVF(CUSTOMFVF);

	// set the view transform
	D3DXMATRIX matView;
	D3DXMatrixLookAtLH(&matView,
		&D3DXVECTOR3 (0.0f, 2.0f, 6.0f),    // the camera position
		&D3DXVECTOR3 (0.0f, 0.0f, 0.0f),      // the look-at position
		&D3DXVECTOR3 (0.0f, 1.0f, 0.0f));    // the up direction
	g_Render->GetDevice()->SetTransform(D3DTS_VIEW, &matView);

	// set the projection transform
	D3DXMATRIX matProjection;
	D3DXMatrixPerspectiveFovLH(&matProjection,
		D3DXToRadian(45),
		(FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT,
		1.0f,    // the near view-plane
		100.0f);    // the far view-plane
	g_Render->GetDevice()->SetTransform(D3DTS_PROJECTION, &matProjection);

	// set the world transform
	static float index = 0.0f; index+=0.03f;
	D3DXMATRIX matRotateY;
	D3DXMatrixRotationY(&matRotateY, index);
	g_Render->GetDevice()->SetTransform(D3DTS_WORLD, &(matRotateY));

	// select the vertex and index buffers to use
	//d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
	//d3ddev->SetIndices(i_buffer);

	// set the texture
	//d3ddev->SetTexture(0, texture);

	// draw the teapot
	meshTeapot->DrawSubset(0);    
	//d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12); 

	g_Render->GetDevice()->EndScene(); 

	g_Render->GetDevice()->Present(NULL, NULL, NULL, NULL);

}
void sa_TwMeshLoad::InitLight()
{
	D3DLIGHT9 light;    // create the light struct
	D3DMATERIAL9 material;    // create the material struct

	ZeroMemory(&light, sizeof(light));    // clear out the light struct for use
	light.Type = D3DLIGHT_DIRECTIONAL;    // make the light type 'directional light'
	light.Diffuse = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f);    // set the light's color
	light.Direction = D3DXVECTOR3(-1.0f, -0.3f, -1.0f);

	g_Render->GetDevice()->SetLight(0, &light);    // send the light struct properties to light #0
	g_Render->GetDevice()->LightEnable(0, TRUE);    // turn on light #0

	ZeroMemory(&material, sizeof(D3DMATERIAL9));    // clear out the struct for use
	material.Diffuse = D3DXCOLOR(1.0f, 0.0f, 1.0f, 1.0f);    // set diffuse color to white
	material.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);    // set ambient color to white

	g_Render->GetDevice()->SetMaterial(&material);    // set the globably-used material to &material

}
void sa_TwMeshLoad::Clear()
{
	meshTeapot->Release();

}